package com.hxd.otc.module.my.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.my.bean.ExchangeRecordBean;
import com.hxd.otc.module.my.contract.ExchangeDetailContract;
import com.hxd.otc.module.my.presenter.ExchangeDetailPresenter;
import com.hxd.otc.utils.AddressUtil;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lzy on 2019/1/4 0004.
 * description ${兑换详情}
 */
public class ExchangeDetailActivity extends BaseActivity<ExchangeDetailPresenter> implements ExchangeDetailContract.IView {
    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.tv_in_Volume)
    TextView mTvInVolume;
    @BindView(R.id.tv_in_symbol)
    TextView mTvInSymbol;
    @BindView(R.id.tv_status)
    TextView mTvStatus;
    @BindView(R.id.tv_type)
    TextView mTvType;
    @BindView(R.id.tv_swap_out)
    TextView mTvSwapOut;
    @BindView(R.id.tv_swap_in)
    TextView mTvSwapIn;
    @BindView(R.id.tv_fee)
    TextView mTvFee;
    @BindView(R.id.tv_exchange_time)
    TextView mTvExchangeTime;
    @BindView(R.id.tv_exchange_number)
    TextView mTvExchangeNumber;

    public static final String EXTRA_EXCHANGE_RECORD = "exchange_record";
    public static final String EXTRA_EXCHANGE_ID = "exchange_id";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_exchange_detail;
    }

    @Override
    protected ExchangeDetailPresenter createPresenter() {
        return new ExchangeDetailPresenter(this);
    }

    @Override
    protected void initView() {
        String orderId = getBundle().getString(EXTRA_EXCHANGE_ID);
        if (!TextUtils.isEmpty(orderId)) {
            mPresenter.queryOrderDetail(orderId);
        } else {
            ExchangeRecordBean exchangeRecordBean = (ExchangeRecordBean) getBundle().getSerializable(EXTRA_EXCHANGE_RECORD);
            updateUI(exchangeRecordBean);
        }
    }

    private void updateUI(ExchangeRecordBean exchangeRecordBean) {
        if (exchangeRecordBean == null) return;
        mTvInVolume.setText(String.format("+%s", StringUtil.format8Number(exchangeRecordBean.getInVolume())));
        mTvInSymbol.setText(exchangeRecordBean.getInSymbol());
        int status = exchangeRecordBean.getStatus();
        String[] statusType = UIUtils.getContext().getResources().getStringArray(R.array.exchange_order_statues);
        if (status >= 0 && status < statusType.length) {
            mTvStatus.setText(statusType[status]);
        }
        mTvSwapOut.setText(String.format("%s %s", StringUtil.format8Number(exchangeRecordBean.getOutVolume()), exchangeRecordBean.getOutSymbol()));
        mTvSwapIn.setText(String.format("%s %s", StringUtil.format8Number(exchangeRecordBean.getInVolume()), exchangeRecordBean.getInSymbol()));
        mTvFee.setText(String.format("%s %s", StringUtil.format8Number(exchangeRecordBean.getFee()), exchangeRecordBean.getInSymbol()));
        mTvExchangeTime.setText(exchangeRecordBean.getCreateDate());
        mTvExchangeNumber.setText(exchangeRecordBean.getId());
    }

    @OnClick({R.id.tv_exchange_number})
    public void onClick(View view) {
        AddressUtil.copyAddress(this, mTvExchangeNumber.getText().toString(), getString(R.string.the_exchange_number_was_copied_successfully));
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
    }

    @Override
    public void queryOrderDetailSuccess(ExchangeRecordBean exchangeRecordBean) {
        updateUI(exchangeRecordBean);
    }
}
